package com.ljf.framework.filter;

import com.ljf.framework.router.LjfRouterManager;

import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 说明：拦截器管理器
 *
 * @Auther: lijinfeng
 * @Date: 2024/4/23
 */
public class LjfFilterManager {

    private static boolean isInit = false;

    private static List<LjfFilter> ljfFilterList = new ArrayList<>();

    public static List<LjfFilter> getLjfFilterList() {
        return ljfFilterList;
    }

    public static void addLjfFilter(LjfFilter ljfFilter) {
        LjfFilterManager.ljfFilterList.add(ljfFilter);
    }

    public static boolean doLjfFilter() {
        if(!isInit){
            loadFilter();
            isInit = true;
        }
        // 标记是否终止
        AtomicBoolean suc = new AtomicBoolean(true);

        for (LjfFilter filter : ljfFilterList) {
            // 执行全局过滤器
            LjfRouterManager.createRouter().match(filter.getIncludeList()).notMatch(filter.getExcludeList()).check(r -> {

                try {
                    if (filter.getBeforeAuthStrategy().run(null)) {
                        boolean toRun = filter.getAuthStrategy().run(null);
                        if (!toRun){
                            suc.set(false);
                        }
                    }
                } catch (Exception e) {
                    filter.getErrorStrategy().run(e);
                    suc.set(false);
                }
            });
            if (!suc.get())
                break;
        }
        return suc.get();
    }

    public static void loadFilter() {
        ServiceLoader<LjfFilter> serviceLoader = ServiceLoader.load(LjfFilter.class);
        //加载的是多个服务,遍历每个服务
        for (LjfFilter service : serviceLoader) {
            //取出一个服务，启动服务
            LjfFilterManager.addLjfFilter(service);
        }
        // 拦截器排序
        ljfFilterList.sort(Comparator.comparingInt(LjfFilter::getOrder));
    }

    public static void clearFilter() {
        ljfFilterList.clear();
    }
//    public static void setLjfFilter(LjfFilter ljfFilter) {
//        LjfFilterManager.ljfFilter = ljfFilter;
//    }
//
//    public static LjfFilter getLjfFilter() {
//        if (ljfFilter == null) {
//            synchronized (LjfFilterManager.class) {
//                if (ljfFilter == null) {
//                    setLjfFilter(new LjfAbstractFilter() {
//                        @Override
//                        public boolean doCusFilter() {
//                            return false;
//                        }
//                    });
//                }
//            }
//        }
//        return ljfFilter;
//    }
//    public static <T extends LjfFilter>LjfFilter getLjfFilter( Class<T> t) throws IllegalAccessException, InstantiationException {
//        if (ljfFilter == null) {
//            synchronized (LjfFilterManager.class) {
//                if (ljfFilter == null) {
//                    setLjfFilter(t.newInstance());
//                }
//            }
//        }
//        return ljfFilter;
//    }
}
